package com.liangzhen.verynews.adapter;

import java.util.ArrayList;
import com.liangzhen.verynews.R;
import com.liangzhen.verynews.bean.Game;
import com.liangzhen.verynews.utils.ImageCache;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("SimpleDateFormat")
public class GameAdapter extends BaseAdapter {

	private ArrayList<Game> newslist = new ArrayList<Game>();
	private Context context;
	private LruCache<String, Bitmap> lruCache;

	public GameAdapter(Context context) {
		super();
		this.context = context;
		lruCache = ImageCache.GetLruCache(context);
	}

	public void BindData(ArrayList<Game> newslist) {
		this.newslist = newslist;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return newslist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return newslist.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.verynews_main_listview_item, null, false);
			viewHolder.biaoti = (TextView) convertView
					.findViewById(R.id.news_item_textView_title);
			viewHolder.source = (TextView) convertView
					.findViewById(R.id.news_item_click_textView);
			viewHolder.tupian = (ImageView) convertView
					.findViewById(R.id.news_item_image);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.biaoti.setText(newslist.get(position).getTitle());
		viewHolder.source.setText(newslist.get(position).getSource());
		viewHolder.tupian.setImageResource(R.drawable.ic_launcher);
		viewHolder.tupian.setTag(newslist.get(position).getImageUrl());
		new ImageCache(context, lruCache, viewHolder.tupian, newslist.get(
				position).getImageUrl(), "News", 120, 90);
		return convertView;
	}

	class ViewHolder {
		TextView biaoti;
		TextView source;
		ImageView tupian;
	}

}
